package com.umeox.watch.moto.dataservice.proxy;

import android.os.IBinder;
import android.os.RemoteException;

import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.interfaces.IPair;

import net.vidageek.mirror.dsl.Mirror;

public class PairProxy implements IPair {
    private IBinder mRemote;

    private PairProxy(IBinder remote) {
        mRemote = remote;
    }

    public static IPair fetchBinder() {
        try {
            return new PairProxy((IBinder) new Mirror().on("android.os.ServiceManager").invoke().method("getService").withArgs(BuildConfig.pairBinderName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IBinder asBinder() {
        return mRemote;
    }


    @Override
    public int makeFriends(String selfMac, String[] nearByMacs) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        int result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(selfMac);
            data.writeStringArray(nearByMacs);
            mRemote.transact(TRANSACTION_MAKEFRIENDS, data, reply, 0);
            reply.readException();
            result = reply.readInt();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }
}
